#include "MainPage.h"
#include "../includes.h"

void unifiedMenuCallBack(void){
  KEY_VALUES key_num = menuKeyGetValue();
  switch(key_num)
  {
    case KEY_ICON_0:
      infoMenu.menu[++infoMenu.cur] = menuUnifiedHeat;
      break;

    case KEY_ICON_1:
      infoMenu.menu[++infoMenu.cur] = menuUnifiedMove;
      break;

    case KEY_ICON_2:
      infoMenu.menu[++infoMenu.cur] = menuExtrude;
      break;

    case KEY_ICON_3:
      // Emergency Stop : Used for emergency stopping, a reset is required to return to operational mode.
      // it may need to wait for a space to open up in the command queue.
      // Enable EMERGENCY_PARSER in Marlin Firmware for an instantaneous M112 command.
      // Serial_Puts(SERIAL_PORT, "M112\n");
      MarlinAPI_EMStop();
      break;

    case KEY_ICON_4:
      infoMenu.menu[++infoMenu.cur] = menuSendGcode;
      break;

    case KEY_ICON_5:
      infoMenu.menu[++infoMenu.cur] = menuCustom;
      break;

    case KEY_ICON_6:
      infoMenu.menu[++infoMenu.cur] = menuSettings;
      break;

    case KEY_ICON_7:
      infoMenu.cur--;
      break; 

    default:
      break;
  }
  loopFrontEnd();
}

void unifiedMenu(void)
{
  // 1 title, ITEM_PER_PAGE items(icon+label)
  const MENUITEMS unifiedPageItems = {
    // title
    LABEL_MAINMENU,
    // icon                         label
    {{ICON_HEAT_FAN,                LABEL_UNIFIEDHEAT},
     {ICON_HOME_MOVE,               LABEL_UNIFIEDMOVE},
     {ICON_EXTRUDE,                 LABEL_EXTRUDE},
     {ICON_STOP,                    LABEL_EMERGENCYSTOP},
     {ICON_GCODE,                   LABEL_TERMINAL},
     {ICON_CUSTOM,                  LABEL_CUSTOM},
     {ICON_SETTINGS,                LABEL_SETTINGS},
     {ICON_BACK,                    LABEL_BACK},}
  };
  GUI_SetBkColor(infoSettings.bg_color);
  menuDrawPage(&unifiedPageItems);
  menuSetFrontCallBack(unifiedMenuCallBack);
}

void classicMenuCallBack(void)
{
  KEY_VALUES key_num = menuKeyGetValue();
  switch(key_num)
  {
    case KEY_ICON_0:
      infoMenu.menu[++infoMenu.cur] = menuUnifiedHeat;
      break;

    case KEY_ICON_1:
      infoMenu.menu[++infoMenu.cur] = menuMove;
      break;

    case KEY_ICON_2:
      infoMenu.menu[++infoMenu.cur] = menuHome;
      break;

    case KEY_ICON_3:
      infoMenu.menu[++infoMenu.cur] = menuPrint;
      break;

    case KEY_ICON_4:
      infoMenu.menu[++infoMenu.cur] = menuExtrude;
      break;

    case KEY_ICON_5:
      infoMenu.menu[++infoMenu.cur] = menuSendGcode;
      break;

    case KEY_ICON_6:
      infoMenu.menu[++infoMenu.cur] = menuSettings;
      break;

    case KEY_ICON_7:
      if(infoMachineSettings.autoLevel == 1)
      {
        infoMenu.menu[++infoMenu.cur] = menuAutoLeveling;
      }
      else
      {
        infoMenu.menu[++infoMenu.cur] = menuManualLeveling;
      }
      break;

    default:
      break;
  }
  loopFrontEnd();
}

void classicMenu(void)
{
  // 1 title, ITEM_PER_PAGE items(icon+label)
  MENUITEMS classicPageItems = {
    // title
    LABEL_READY,
    // icon                         label
    {{ICON_HEAT_FAN,                LABEL_UNIFIEDHEAT},
     {ICON_MOVE,                    LABEL_MOVE},
     {ICON_HOME,                    LABEL_HOME},
     {ICON_PRINT,                   LABEL_PRINT},
     {ICON_EXTRUDE,                 LABEL_EXTRUDE},
     {ICON_GCODE,                   LABEL_TERMINAL},
     {ICON_SETTINGS,                LABEL_SETTINGS},
     {ICON_LEVELING,                LABEL_LEVELING},}
  };

  if (infoMachineSettings.autoLevel == 1)
  {
    classicPageItems.items[7].icon = ICON_LEVELING;
    classicPageItems.items[7].label.index = LABEL_ABL;
  }
  else
  {
    classicPageItems.items[7].icon = ICON_MANUAL_LEVEL;
    classicPageItems.items[7].label.index = LABEL_LEVELING;
  }

  GUI_SetBkColor(infoSettings.bg_color);

  menuDrawPage(&classicPageItems);

  menuSetFrontCallBack(classicMenuCallBack);
}
